/*
 * test.c
 *
 *  Created on: May 31, 2015
 *      Author: root
 */

#include <gtk/gtk.h>

union GUI{
	GObject * gObject;
	GtkObject * gtkObject;
	GtkWidget * gtkWidget;
	GtkContainer * gtkContainer;
	GtkBin * gtkBin;
	GtkWindow * gtkWindow;
	GtkButton * gtkButton;
};

typedef struct _DATA{
	gchar *name;
	gpointer callback_data;
}DATA;

/* Stop the GTK+ main loop function. */
static void
destroy (GtkWidget *window,
         gpointer data)
{
  gtk_main_quit ();
}

static void
widget_destroy (gpointer data, GtkWidget *window)
{
	DATA *dd = (DATA *)data;
	GtkWidget * win = (GtkWidget *)dd->callback_data;
	union GUI w;
	w.gtkWidget = win;
	w.gtkWindow = GTK_WINDOW(w.gtkWidget);
	w.gtkWidget = GTK_WIDGET(w.gtkWindow);
  gtk_widget_destroy (w.gtkWidget);
}

int main (int argc,
          char *argv[])
{
	union GUI window, button;

	//GtkWidget *window, *button;

  //gtk_init (&argc, &argv);
  gtk_init (0, NULL);

  window.gtkWidget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  window.gtkWindow = GTK_WINDOW (window.gtkWidget);
  gtk_window_set_title (window.gtkWindow, "Buttons");
  window.gtkContainer = GTK_CONTAINER (window.gtkWidget);
  gtk_container_set_border_width (window.gtkContainer, 25);
  gtk_widget_set_size_request (window.gtkWidget, 200, 100);

  g_signal_connect (G_OBJECT (window.gtkWidget), "destroy",
                    G_CALLBACK (destroy), NULL);


    /* Create a new button that has a mnemonic key of Alt+C. */
    button.gtkWidget = gtk_button_new_with_mnemonic ("_Close");
    button.gtkButton = GTK_BUTTON (button.gtkWidget);
    gtk_button_set_relief (button.gtkButton, GTK_RELIEF_NONE);

    DATA *data = (DATA *)malloc(sizeof(DATA));
    data->callback_data = window.gtkWidget;
    data->name = "widget_destroy";

    /* Connect the button to the clicked signal. The callback function recieves the
     * window followed by the button because the arguments are swapped. */
    g_signal_connect_swapped (G_OBJECT (button.gtkWidget), "clicked",
                              G_CALLBACK (widget_destroy),
                              (gpointer) data);

    gtk_container_add (GTK_CONTAINER (window.gtkWidget), button.gtkWidget);

  gtk_widget_show_all (window.gtkWidget);

  gtk_main ();
  return 0;
}

/*
{init gui environment}
{define {myWindow} as {create new window at toplevel}}
;{define {myWindow} as {convert widget {myWidget} to window}}
{define function {destroy}: {destory {window} with {data}} as {gui quit}}
{define function {widget-destroy}: {destroy the widget {widget} with {data}} as {destroy widget {widget}}}
{set title {"Buttons"} for {myWindow}}
{connect signal {"destroy"} to {myWindow} with the function {"destroy"} and data {'()}}
{define {myButton} as {create new button with mnemonic {"_Close"}}}
;{define {myButton} as {convert widget {buttonWidget} to button}}
{set relief none for button {myButton}}
{connect signal swapped {"clicked"} to {myButton} with the function {"widget-destroy"} and data {myWindow}}
{add {myButton} to container {myWindow}}
{show all {myWindow}}
{gui start}
 * */

